# pymysql
# pip install pymysql

# 用于做MYSQL连接
from pymysql import connect

'''
        user=None,  # The first four arguments is based on DB-API 2.0 recommendation.
        password="",
        host=None,
        database=None,
        port=0,
        charset="",
'''
# con = connect(user='root', password='123456', host='192.168.116.90', database='spider', port=3306, charset='utf8')
# cur = con.cursor()
# # execute方法并不会直接执行，需要commit提交至数据库执行
# cur.execute("INSERT INTO test (id,name) values (2,'李四')")
# con.commit()
# con.close()

# MYSQL数据查询

# con = connect(user='root', password='123456', host='192.168.116.90', database='spider', port=3306, charset='utf8')
# cur = con.cursor()
# # result 返回结果的条数
# result = cur.execute("select * from test")
# con.commit()
#
# for row in range(result):
#     print("获取到第{}条数据:{}".format(row+1, cur.fetchone()))
#
# con.close()


# MYSQL批量操作

con = connect(user='root', password='123456', host='192.168.116.90', database='spider', port=3306, charset='utf8')
cur = con.cursor()

rows = [
    (3, "王五"),
    (4, "老六"),
    (5, "老八")
]

cur.executemany(
    "INSERT INTO test (id,name) values (%s,%s)"
    , rows
)
con.commit()
